package com.bytz.modules.cms.order.controller;

import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.domain.model.pay.BalancePayIn;
import com.bytz.modules.cms.domain.model.pay.PayOut;
import com.bytz.modules.cms.domain.model.pay.PrepayIn;
import com.bytz.modules.cms.order.model.GetOrderPayListOut;
import com.bytz.modules.cms.order.service.IMallOrderPayService;
import com.bytz.modules.cms.reseller.service.IResellerService;
import com.bytz.modules.cms.reseller.util.ResellerSecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;

@RestController
@Api(tags = "支付")
@RequestMapping("/pay")
public class OrderPayController {

    @Autowired
    private IMallOrderPayService orderPayService;

    @Autowired
    private IResellerService resellerService;


    @ApiOperation("根据订单id获取支付信息")
    @RequestMapping(value = "/listByOrder", method = RequestMethod.GET)
    public Result<GetOrderPayListOut> listByOrder(@RequestParam String orderId) {
        return Result.ok(orderPayService.getByOrderId(orderId));
    }

    @ApiOperation("根据订单id获取预付金额")
    @RequestMapping(value = "/getPrepayPrice", method = RequestMethod.GET)
    public Result<BigDecimal> getPrepayPrice(@RequestParam String orderId) {
        return Result.ok(orderPayService.calculatePrepayPrice(orderId));
    }

    @ApiOperation("支付预付款准备")
    @RequestMapping(value = "prepay", method = RequestMethod.POST)
    public Result<PayOut> prepayPrepare(@RequestBody @Valid PrepayIn prepay) {
        return Result.ok(orderPayService.prepay(prepay));
    }

    @ApiOperation("尾款准备")
    @RequestMapping(value = "balancePay", method = RequestMethod.POST)
    public Result<PayOut> balancePayPrepare(@RequestBody @Valid BalancePayIn balance) {
        return Result.ok(orderPayService.balancePay(balance));
    }

    @ApiOperation("是否需要输入支付口令")
    @RequestMapping(value = "/needPaymentPassword", method = RequestMethod.GET)
    public Result<Boolean> needPaymentPassword() {
        return Result.ok(resellerService.needPaymentPassword(ResellerSecurityUtils.getUserId()));
    }
}
